<?php 

require ("sharedResources.php");

function downloadDocumentsInFolder($sid, $folderId, $folderPath) {
	
	global $documentClient;

    $cfp = array ('sid' => $sid, 'folderId' => $folderId );

	$result = $documentClient->list ( $cfp );
	//print_r ( $result );
	
	if (! empty ( $result->document )) {
		$documents = $result->document;
		if (! is_array ( $documents )) {
			$documents = array ();
			$documents [0] = $result->document;
		}
		
		for($i = 0; $i < sizeof ( $documents ); $i ++) {
			$document = $documents [$i];
			echo 'docId: ' . $document->id . PHP_EOL;
			echo 'Title: ' . $document->title . PHP_EOL;
			echo 'FileName: ' . $document->fileName . PHP_EOL;
			echo '----------------' . PHP_EOL;
			
			$docId = $document->id;
			$myFileName = $document->fileName;
			//print_r ( 'fileName: '.$myFileName .PHP_EOL);			

			// 2) Then download the document (binary content)
			$ddp = array ('sid' => $sid, 'docId' => $docId );
			
			$result = $documentClient->getContent ( $ddp );
			
			// get the binary content from the result response
			$content = $result->return;
			
			// Finally write the document on disk
			$myFilePath = $folderPath . "/" . $myFileName;
			echo 'Writing content to: ' . $myFilePath . PHP_EOL;
			
			$fh = fopen ( $myFilePath, 'w' ) or die ( "can't open file" );
			fwrite ( $fh, $content );
			fclose ( $fh );
		}
	}
}


function listFoldersRecursive($sid, $parentFolder, $basePath) {
	
	global $folderClient;
	
	$cfp = array ('sid' => $sid, 'folderId' => $parentFolder );
	
	// Get the data of the current folder
	$result = $folderClient->getFolder ( $cfp );
	
	$currentFolder = $result->folder;
	
	// Create path for the current folder
	$parentPath = $basePath . "/" . $currentFolder->name;
	if (! is_dir ( $parentPath )) {
		if (mkdir ( $parentPath, 0777 )) {
			echo "Directory has been created successfully..." . PHP_EOL;
		} else {
			echo "Failed to create directory..." . PHP_EOL;
		}
	} 
	
	// Download documents in the current folder to disk	
	downloadDocumentsInFolder($sid, $parentFolder, $parentPath);
	
	// Get folder children 
	$result = $folderClient->listChildren ( $cfp );
	
	// Cycle over the results and download all the documents to their folders
	if (! empty ( $result->folder )) {
		$folders = $result->folder;
		if (! is_array ( $folders )) {
			$folders = array ();
			$folders [0] = $result->folder;
		}
		
		for($i = 0; $i < sizeof ( $folders ); $i ++) {
			$folder = $folders [$i];
			echo 'folderId: ' . $folder->id . PHP_EOL;
			echo 'Name: ' . $folder->name . PHP_EOL;
			echo '----------------' . PHP_EOL;
			listFoldersRecursive ( $sid, $folder->id, $parentPath );
		}
	}
}

$sid = '';
try {
	$sid = login ( 'admin', 'admin' );
	echo 'sid: ' . $sid . PHP_EOL;
	
	// This is the ID of the initial folder to explore
	//$folderID = 3604482;
  $folderID = 5; // start from the default workspace
	$basePath = "C:/tmp";
	listFoldersRecursive($sid, $folderID, $basePath);

} catch ( SoapFault $e ) {
	print_r ( $e );
}

// Always we need to logout in order to release connection slot
logout ( $sid );

?> 
